<?php 

	function stripNonAlpha($str)
	{
		$str = preg_replace("/[^a-zA-Z\s]/", "", $str);
		return strip_tags(trim($str));
	}
	
	function stripNonAlphaNum($str)
	{
		$str = preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
		return strip_tags(trim($str));
	}
	
	function stripNum($val)
	{
		$out = eregi_replace("([0-9]+)","",$val);
		return ($out);
	}
	
	function stripAlpha($val)
	{
		$out = eregi_replace("([A-Z]+)","",$val);
		return $out;
	}
	
	function stripSpace($val)
	{
		str_replace(' ','',$val);
		return $val;
	}
	
	function hasSpace($val)
	{
		if( ereg("[ 	]",$val) )
		{ return true; }
		else
		{ return false;	}
	}
	
	function isAllNum($val)
	{
		if((gettype($val)) == "integer")	
		{ return true; }
		return false;
	}
	
	function isEmail($email)
	{
	   $isValid = true;
	   $atIndex = strrpos($email, "@");
	   if (is_bool($atIndex) && !$atIndex)
	   { $isValid = false; }
	   else
	   {
		  $domain = substr($email, $atIndex+1);
		  $local = substr($email, 0, $atIndex);
		  $localLen = strlen($local);
		  $domainLen = strlen($domain);
		  if ($localLen < 1 || $localLen > 64)
		  { $isValid = false; }
		  else if ($domainLen < 1 || $domainLen > 255)
		  { $isValid = false; }
		  else if ($local[0] == '.' || $local[$localLen-1] == '.')
		  { $isValid = false; }
		  else if (preg_match('/\\.\\./', $local))
		  { $isValid = false; }
		  else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
		  { $isValid = false;  }
		  else if (preg_match('/\\.\\./', $domain))
		  { $isValid = false; }
		  else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
		  {
			 // character not valid in local part unless 
			 // local part is quoted
			 if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
			 { $isValid = false; }
		  }
		  /*
		  if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
		  {
			 // domain not found in DNS
			 $isValid = false;
		  }
		  */
	   }
	   return $isValid;
	}


	function escape($string) 
	{
		if(get_magic_quotes_gpc()) $string = stripslashes($string);
		return mysql_real_escape_string($string);
	}
	
	function dbVal($val, $type)
	{
		switch ($type)
		{
			case 'int':
				if(is_numeric($val))
				{ return $val; }
				else
				{ return 0; }
			case 'txt':
				return '"'.$val.'"';
		}
	}
?>